package logic

import (
	"context"
	"fmt"
	"math/rand"
	"strconv"
	"uuser/models/cache"
	"uuser/pkg"
	"uuser/uuserrpc/internal/svc"
	"uuser/uuserrpc/uuserrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendSmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLogic {
	return &SendSmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SendSmsLogic) SendSms(in *uuserrpc.SendSmsRequest) (*uuserrpc.SendSmsResponse, error) {
	// todo: add your logic here and delete this line
	code := rand.Intn(9000) + 1000
	sms, err := pkg.SendSms(in.Mobile, strconv.Itoa(code))
	if err != nil {
		return nil, err
	}
	if *sms.Body.Code != "OK" {
		return nil, fmt.Errorf(*sms.Body.Message)
	}
	cache.Rdb.Set(cache.Ctx, "send"+in.Mobile, code, 0)
	return &uuserrpc.SendSmsResponse{}, nil
}
